package com.manager.util;

import java.io.Serializable;

/*
 * 拼字符串时常用到的一个类，由StringBuffer扩展而来，很实用
 * @author collonn
 */
public class Stringbuffer {
	private StringBuffer sb;

	private StringBuffer backup;

	private int offset;

	public Stringbuffer() {
		sb = new StringBuffer();
	}

	/*
	 * 可以是直接量(1, 3.6,"obj"等)，包装类(String.valueOf(),Long.valueOf(),Double.valueOf()等)
	 * 但必须是Serializable的实现类
	 */
	public Stringbuffer(Serializable obj) {
		sb = new StringBuffer(obj.toString());
	}

	/*
	 * 查找字符串位置
	 */
	public int indexOf(String str) {
		return sb.indexOf(str);
	}

	/*
	 * 清空数据
	 */
	public Stringbuffer clear() {
		sb.delete(0, sb.length());
		return this;
	}

	/*
	 * 删除从offserStart到offsetEnd的字符
	 */
	public Stringbuffer delete(int offsetStart, int offsetEnd) {
		if(offsetStart == -1 || offsetEnd == -1){
			return this;
		}
		sb.delete(offsetStart, offsetEnd);
		return this;
	}

	/*
	 * 加入到尾
	 */
	public Stringbuffer append(Serializable append) {
		sb.append(append.toString());
		return this;
	}

	/*
	 * 加入到头
	 */
	public Stringbuffer prepend(Serializable prepend) {
		sb.insert(0, prepend.toString());
		return this;
	}

	/*
	 * 在offsetStart位置后插入字符串
	 */
	public Stringbuffer insert(int offsetStart, String str) {
		sb.insert(offsetStart, str);
		return this;
	}

	/*
	 * 备份原数据
	 */
	public Stringbuffer backup() {
		if (backup == null) {
			backup = new StringBuffer();
		}
		backup.delete(0, backup.length()).append(sb.toString());
		return this;
	}

	/*
	 * 还原备份的字符串
	 */
	public Stringbuffer reduce() {
		this.clear().append(backup.toString());
		return this;
	}

	/*
	 * 请确保你的字符中只含有一个oldStr，只替换一次
	 */
	public Stringbuffer replace(String oldStr, String newStr) {
		offset = sb.indexOf(oldStr);
		sb.replace(offset, offset + oldStr.length(), newStr);
		return this;
	}

	/*
	 * 请确保你的字符中只含有一个oldStr，只替换一次， 注：形如replaceAll(new String[]{"-"," ",":","^"},
	 * new String[]{"","?"}) 将这样替换："-"和" "分另用""和"?"替换，","和"^"将以"?"替换(即new
	 * String[]的最后一个)
	 */
	public Stringbuffer replace(String[] oldStr, String[] newStr) {
		for (int i = 0; i < oldStr.length; i++) {
			if (i < newStr.length) {
				this.replace(oldStr[i], newStr[i]);
			} else {
				this.replace(oldStr[i], newStr[newStr.length - 1]);
			}
		}
		return this;
	}

	/*
	 * 无论你的字符中含多少个oldStr，都替换成newStr
	 */
	public Stringbuffer replaceAll(String oldStr, String newStr) {
		offset = sb.indexOf(oldStr);
		while (offset != -1) {
			sb.replace(offset, offset + oldStr.length(), newStr);
			offset = sb.indexOf(oldStr);
		}
		return this;
	}

	/*
	 * 无论你的字符中含多少个oldStr，都替换成newStr 注：形如replaceAll(new String[]{"-","
	 * ",":","^"}, new String[]{"","?"}) 将这样替换："-"和"
	 * "分另用""和"?"替换，","和"^"将以"?"替换(即new String[]的最后一个)
	 */
	public Stringbuffer replaceAll(String[] oldStr, String[] newStr) {
		for (int i = 0; i < oldStr.length; i++) {
			if (i < newStr.length) {
				this.replaceAll(oldStr[i], newStr[i]);
			} else {
				this.replaceAll(oldStr[i], newStr[newStr.length - 1]);
			}
		}
		return this;
	}

	/*
	 * 如果搜索到source字符串，从source字符串结束位置开始，向右搜索length个字符，目标字串符为str
	 */
	public int searchRight(String source, int length, String target) {
		if (this.indexOf(source) == -1) {
			return -1;
		}

		int start = this.indexOf(source) + source.length();
		int end = start + length;
		if (end > sb.length()) {
			end = sb.length();
		}
		String temp = sb.substring(start, end);
		if (temp.indexOf(target) != -1) {
			start = start + temp.indexOf(target);
		} else {
			start = -1;
		}
		return start;
	}

	/*
	 * 如果搜索到source字符串，从source字符串开始位置开始，向左搜索length个字符，目标字串符为str
	 */
	public int searchLeft(String source, int length, String target) {
		if (this.indexOf(source) == -1) {
			return -1;
		}

		int end = this.indexOf(source);
		int start = end - length;
		if (start < 0) {
			start = 0;
		}
		String temp = sb.substring(start, end);
		if (temp.indexOf(target) != -1) {
			start = start + temp.indexOf(target);
		} else {
			start = -1;
		}

		return start;
	}

	/*
	 * 删除字符串str所在的一行
	 * 注：windows以"\r\n"换行；linux以\n换行；Mac平台以\r换行；Unicode标准以\u2028换行
	 */
	public Stringbuffer deleteLine(String str) {
		this.delete(this.beforeSearch(str, "\r\n") + 2, this.afterSearch(str, "\r\n") + 2);
		this.delete(this.beforeSearch(str, "\n") + 1, this.afterSearch(str, "\n") + 1);
		this.delete(this.beforeSearch(str, "\r") + 1, this.afterSearch(str, "\r") + 1);
		this.delete(this.beforeSearch(str, "\u2028") + 1, this.afterSearch(str, "\u2028") + 1);
		return this;
	}

	/*
	 * 从offsetStr字符串所在位置开始，向前搜索第一个target所在的位置
	 */
	public int beforeSearch(String offsetStr, String target) {
		if(sb.indexOf(offsetStr) == -1){
			return -1;
		}
		return sb.substring(0, sb.indexOf(offsetStr)).lastIndexOf(target);
	}

	/*
	 * 从offsetStr字符串所在位置开始，向后搜索第一个target所在的位置
	 */
	public int afterSearch(String offsetStr, String target) {
		if(sb.indexOf(offsetStr) == -1){
			return -1;
		}
		return sb.indexOf(target, sb.indexOf(offsetStr));
	}

	public String toString() {
		return sb.toString();
	}

	public static void main(String[] args) {
		String str = "28^5,6,8|27^9";
		String[] t = str.split(",");
		Log.info("==>" + t.length);
	}
}
